package com.yinp.proapp.activity;

import android.animation.Animator;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.viewbinding.ViewBinding;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.yinp.proapp.R;
import com.yinp.proapp.activity.bean.MyUserInfo;
import com.yinp.proapp.base.App;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.constant.SpConstants;
import com.yinp.proapp.databinding.ActivitySplashBinding;
import com.yinp.proapp.databinding.DialogTipBinding;
import com.yinp.proapp.utils.AppUtils;
import com.yinp.proapp.utils.JPushUtils;
import com.yinp.proapp.utils.LoadUtils;
import com.yinp.proapp.utils.SpUtils;
import com.yinp.tools.fragment_dialog.BaseDialogFragment;
import com.yinp.tools.fragment_dialog.CommonDialogFragment;
import com.yinp.tools.fragment_dialog.DialogFragmentHolder;
import com.yinp.tools.fragment_dialog.ViewConvertListener;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jpush.android.api.JPushInterface;
import io.rong.imlib.RongIMClient;

/**
 * 启动页
 */
public class SplashActivity extends AppBaseActivity<ActivitySplashBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppUtils.setFullScreen(getWindow());
    }

    @Override
    protected void initViews() {
        Log.d(TAG, "initViews: "+SpUtils.getValue(SpConstants.IS_FIRST, false));
        if (!SpUtils.getValue(SpConstants.IS_FIRST, false)) {
            privacyPolicy();
        }else {
            initView();
        }
    }
    private void initView(){
        initSdk();
        bd.lavContent.playAnimation();
        JPushUtils.initChannel(this);
        if (!SpUtils.getValue(SpConstants.IS_CREATE_NOTIFICATION_CHANNEL, false)) {
        }
        bd.lavContent.addAnimatorListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (MyUserInfo.isLogin()) {
                    setToken();
                    goToActivity(MajorActivity.class);
                } else {
                    goToActivity(LoginActivity.class);
                }
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }
    private void initSdk(){
        SDKInitializer.initialize(App.appContext);
        SDKInitializer.setCoordType(CoordType.BD09LL);
        RongIMClient.init(App.appContext, "kj7swf8oks4k2", true);
        //极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(App.appContext);
        //极光分享
        JShareInterface.init(App.appContext);
        JShareInterface.setDebugMode(true);
        Bmob.initialize(App.appContext, "dd5c3ca2c53abca9cc19218b7bb9ab90");
    }
    private void setToken() {
        MyUserInfo myUserInfo = MyUserInfo.getCurrentUser(MyUserInfo.class);
        if (TextUtils.isEmpty(myUserInfo.getToken())) {
            myUserInfo.setToken("r8YwlEM393CD9V5YlXBTLrtpqT6vz25JxzR2TITq7ro=@ajl1.cn.rongnav.com;ajl1.cn.rongcfg.com");
            myUserInfo.update(myUserInfo.getObjectId(), new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    Log.d(TAG, "done: 添加token成功");
                    SpUtils.saveValue(myUserInfo.getToken(), SpConstants.RY_TOKEN);
                }
            });
        } else {
            SpUtils.saveValue(myUserInfo.getToken(), SpConstants.RY_TOKEN);
        }
    }

    /**
     * 第一次使用显示隐私政策同意弹窗
     */
    private void privacyPolicy() {
        CommonDialogFragment.newInstance().setLayout(DialogTipBinding.inflate(getLayoutInflater()))
                .setViewConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(DialogFragmentHolder holder, BaseDialogFragment dialogFragment, ViewBinding viewBinding) {
                        super.convertView(holder, dialogFragment, viewBinding);
                        DialogTipBinding binding = (DialogTipBinding) viewBinding;
                        binding.tvTitle.setText("隐私政策");
                        binding.tvLeft.setOnClickListener(v -> {
                            dialogFragment.dismiss();
                            SpUtils.saveValue(false, SpConstants.IS_FIRST);
                            finish();
                        });
                        binding.tvRight.setOnClickListener(v -> {
                            dialogFragment.dismiss();
                            SpUtils.saveValue(true, SpConstants.IS_FIRST);
                            initView();
                        });
                    }
                }).setGravity(BaseDialogFragment.CENTER).setAnimStyle(R.style.CenterDialogAnimation)
                .setPercentSize(0.8f, 0f).setIsCancelable(false).show(getSupportFragmentManager());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        LoadUtils.getInstance().setNull();
    }
}